$(function () {
    window.app.page['channel'] = {
        divChannelList: $('#channelList'),
        viewModel: function (data) {
            var self = this;
            self.allChannel = data.data;
            self.rootChannel = $.grep(self.allChannel, function (channel) {
                return channel.parentId == (window.app.util.getUrlParam('parentId') || '0');
            });

            self.clickChannel = function (item) {
                if (item) {
                    if (!$.grep(self.allChannel, function (channel) {
                            return channel.parentId == item.channelId;
                        }).length) {
                        // open doc list
                        window.location.href = '/mobile/index.html?page=docList&parentId=' + item.channelId;
                    } else {
                        // open sub channel
                        window.location.href = '/mobile/index.html?page=channel&parentId=' + item.channelId;
                    }
                }
            }
        }
    };
    window.app.page['channel'].fn = {
        init: function () {
            window.app.util.restClient('/thirdPart/nmgat/webChannels', null, function (data) {
                ko.applyBindings(new window.app.page.channel.viewModel(data), window.app.page.channel.divChannelList[0]);
                $.hideLoading();
            })
        }
    };

    window.app.route.channel.successFun = window.app.page.channel.fn.init;
});